<template>
  <!-- 新增部门的弹层 -->
  <el-dialog :title="showTitle" :visible="showDialog" @close="handleClose">
    <!-- 表单组件  el-form   label-width设置label的宽度   -->
    <!-- 匿名插槽 -->
    <el-form ref="deptForm" :model="form" :rules="rules" label-width="120px">
      <el-form-item label="部门名称" prop="name">
        <el-input
          v-model="form.name"
          style="width: 80%"
          placeholder="1-50个字符"
        />
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input
          v-model="form.code"
          style="width: 80%"
          placeholder="1-50个字符"
        />
      </el-form-item>
      <el-form-item label="部门负责人" prop="manager">
        <el-select
          v-model="form.manager"
          style="width: 80%"
          placeholder="请选择"
        >
          <el-option
            v-for="item in peoples"
            :key="item.id"
            :value="item.username"
            :label="item.username"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="部门介绍" prop="introduce">
        <el-input
          v-model="form.introduce"
          style="width: 80%"
          placeholder="1-300个字符"
          type="textarea"
          :rows="3"
        />
      </el-form-item>
    </el-form>
    <!-- el-dialog有专门放置底部操作栏的 插槽  具名插槽 -->
    <el-row slot="footer" type="flex" justify="center">
      <!-- 列被分为24 -->
      <el-col :span="6">
        <el-button type="primary" size="small" @click="addForm">确定</el-button>
        <el-button size="small" @click="handleClose">取消</el-button>
      </el-col>
    </el-row>
  </el-dialog>
</template>

<script>
import { getEmployeeSimple } from '@/api/employees'
import {
  addDepartment,
  getDepartDetail,
  updateDepartments
} from '@/api/departments'
export default {
  props: {
    showDialog: {
      type: Boolean,
      default: false
    },
    treeData: {
      type: Object,
      required: true
    },
    list: {
      type: Array,
      required: true
    }
  },
  data() {
    const checkName = (rule, value, callback) => {
      let arr = []
      if (this.form.id) {
        // 编辑状态
        if (value === this.treeData.name) {
          callback()
          return
        }
        arr = this.list.filter((item) => item.pid === this.treeData.id)
      } else {
        // 新增状态
        arr = this.list.filter((item) => item.pid === this.treeData.id)
      }

      // 判断是否有重名
      const flag = arr.some((item) => item.name === value)
      flag ? callback(new Error('同级部门有重名')) : callback()
    }

    const checkCode = (rule, value, callback) => {
      if (this.form.id && value === this.treeData.code) {
        callback()
        return
      }
      // 所有编码都不能重复
      const isRepeat = this.list.some((item) => item.code === value)
      isRepeat ? callback(new Error(`已经有编码${value}的部门了`)) : callback()
    }
    return {
      form: {
        name: '', // 部门名称
        code: '', // 部门编码
        manager: '', // 部门管理者
        introduce: '' // 部门介绍
      },
      // 定义校验规则
      rules: {
        name: [
          {
            required: true,
            message: '部门名称不能为空',
            trigger: ['blur', 'change']
          },
          {
            min: 1,
            max: 50,
            message: '部门名称要求1-50个字符',
            trigger: ['blur', 'change']
          },
          { validator: checkName, trigger: 'blur' }
        ],
        code: [
          {
            required: true,
            message: '部门编码不能为空',
            trigger: ['blur', 'change']
          },
          {
            min: 1,
            max: 50,
            message: '部门编码要求1-50个字符',
            trigger: ['blur', 'change']
          },
          { validator: checkCode, trigger: 'blur' }
        ],
        manager: [
          {
            required: true,
            message: '部门负责人不能为空',
            trigger: ['blur', 'change']
          }
        ],
        introduce: [
          {
            required: true,
            message: '部门介绍不能为空',
            trigger: ['blur', 'change']
          },
          {
            min: 1,
            max: 300,
            message: '部门介绍要求1-300个字符',
            trigger: ['blur', 'change']
          }
        ]
      },
      // 员工列表
      peoples: []
    }
  },
  computed: {
    showTitle() {
      return this.form.id ? '编辑部门' : '新增部门'
    }
  },
  created() {
    this.getEmployeeSimple()
  },
  methods: {
    handleClose() {
      this.$emit('close-dialog')
      // 重置表单
      this.$refs.deptForm.resetFields()
      // 重置数据
      this.form = {
        name: '', // 部门名称
        code: '', // 部门编码
        manager: '', // 管理者
        introduce: '' // 部门介绍
      }
    },
    // 获取部门详情
    async getDepartmentDetail() {
      const { data: res } = await getDepartDetail(this.treeData.id)
      // console.log(res)
      this.form = res
    },
    // 获取员工简单列表数据
    async getEmployeeSimple() {
      const res = await getEmployeeSimple()
      this.peoples = res.data
      console.log(res)
    },
    // 提交表单
    addForm() {
      this.$refs.deptForm.validate(async(valid) => {
        if (!valid) return

        if (this.form.id) {
          // 编辑
          await updateDepartments(this.form)
          this.$message.success('编辑成功')
        } else {
          // 新增
          // ajax提交数据
          await addDepartment({ ...this.form, pid: this.treeData.id })
          // console.log(this.form, '111')
          // console.log(this.treeData.id, '000')
          // 成功提示
          this.$message.success('添加成功')
        }

        // 关闭弹出框
        this.handleClose()
        // 重新获取部门数据
        this.$emit('add-dept')
      })
    }
  }
}
</script>
